<template>
    <div class="light-Ct light-Ct-line"
        :class="{ 'light-Ct-line': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">
        <div class="item-Cd" :class="{ 'item-Cd-line': item.children && item.children.length > 0 }">

            <div class="item-Cd-title">
                <el-popover placement="top" :width="200" trigger="hover">
                    <template #reference>
                        <div class="item-name">
                            <div v-if="!isEditing" @click="startEdit" class="pointers">{{ item.title }}</div>
                            <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit"
                                ref="editInput" style="width: 100%;" />
                        </div>
                    </template>
                    <div class="operation">
                        <el-button link type="primary" @click="addBrotherItem(index, item)">同级 +</el-button>
                        <el-divider direction="vertical" />
                        <el-button link type="primary" @click="addChildItem(index, item)">子级 +</el-button>
                        <el-divider direction="vertical" />
                        <el-button link type="danger" @click="deleteItem(index, item)">删除</el-button>
                        <el-divider v-if="item.children && item.children.length" direction="vertical" />
                        <el-button v-if="item.children && item.children.length" link type="primary"
                            @click="toggleExpand(index, item)">
                            {{ item.expanded ? '收起' : '展开' }}
                        </el-button>
                    </div>
                </el-popover>
            </div>


        </div>
        <div style="display: flex;align-items: center;justify-content: center;">
            <div v-if="item.expanded && item.children && item.children.length"
                :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
                <RecursiveItemRL v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                    :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem"
                    @addChildItem="addChildItem" @deleteItem="deleteItem" :parentChildrenLength="item.children.length"
                    @toggleExpand="toggleExpand" />
            </div>
        </div>

    </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, nextTick } from 'vue';
import RecursiveItemRL from './RecursiveItemRL.vue'; // 确保路径正确

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: {
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'toggleExpand']);

const isEditing = ref(false);
const editedName = ref(props.item.name);
const editInput = ref(null);

const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        editInput.value.focus();
    });
}

const finishEdit = () => {
    isEditing.value = false;
    if (editedName.value !== props.item.name) {
        emit('editItem', props.index, { ...props.item, name: editedName.value });
    }
}

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
}

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
}

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
}

const toggleExpand = (index, item) => {
    emit('toggleExpand', index, item);
}
</script>

<style lang="scss" scoped>
.pointers {
    cursor: pointer;
}

.light-item {

    &.light-itemLeft,
    &.light-itemRight {
        .light-Ct {
            margin: 2px 0px;
            display: flex;
            align-items: center;

            .item-Cd {
                position: relative;

                &.item-Cd-line::after {
                    content: "";
                    position: absolute;
                    right: -25px;
                    top: 50%;
                    width: 25px;
                    height: 1px;
                    background-color: rgb(43, 38, 38);
                }

                .item-name {
                    color: var(--global-font-color);
                    font-size: 12px;
                    padding: 4px 8px;
                    border: 1px solid #ddd;
                    border-radius: 5px;
                    background-color: #f9f9f9;
                    transition: background-color 0.3s;
                    width: auto;
                    display: inline-block;
                }


            }

            .childItem {
                margin-left: 24px;
                position: relative;

                .light-Ct {
                    position: relative;
                }


                .light-Ct-line::after {
                    content: "";
                    position: absolute;
                    left: -24px;
                    top: 0px;
                    width: 1px;
                    height: calc(100% + 2px);
                    background-color: rgb(43, 38, 38);
                }

                >.light-Ct-line:first-child::after {
                    content: "";
                    position: absolute;
                    left: -24px;
                    top: 50%;
                    width: 1px;
                    height: calc(50% + 2px);
                    background-color: rgb(43, 38, 38);
                }

                >.light-Ct-line:last-child::after {
                    content: "";
                    position: absolute;
                    left: -24px;
                    top: 0px;
                    width: 1px;
                    height: 50%;
                    background-color: rgb(43, 38, 38);
                }


            }


            .childItemSon {
                padding-left: 24px;

                &>.light-Ct::before {
                    content: "";
                    position: absolute;
                    left: -24px;
                    top: 50%;
                    width: 24px;
                    height: 1px;
                    background-color: rgb(43, 38, 38);
                }

            }


        }
    }

    &.light-itemRight {
        transform: rotate(180deg) scaleY(-1);

        .pointers {
            transform: rotate(180deg) scaleY(-1);
        }
    }
}
</style>
